package com.gt.quality.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.quality.base.CacheNames;
import com.gt.quality.config.SpringBeanUtil;
import com.gt.quality.system.entity.SysDictData;
import com.gt.quality.system.mapper.SysDictDataMapper;
import com.gt.quality.base.pojo.BaseLimitRequest;
import com.gt.quality.system.service.SysDictDataService;
import jakarta.annotation.Resource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

/**
 * 字典数据表(SysDictData)表服务实现类
 *
 * @author makejava
 * @since 2026-12-26 11:20:03
 */
@Service("sysDictDataService")
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {

    @Resource
    private SysDictDataMapper sysDictDataMapper;


    /**
     * Description:  分页查询
     *
     * @param limitRequest 分页查询参数
     * @author: makeJava
     * @date: 2026-12-26 11:20:03
     */
    @Override
    public IPage<SysDictData> queryLimitPage(BaseLimitRequest<SysDictData> limitRequest) {
        SysDictData requestBody = limitRequest.getRequestBody();
        IPage<SysDictData> page = getByPageParam(limitRequest);
        JSONObject thisObj = JSONObject.from(requestBody);
        QueryWrapper<SysDictData> queryWrapper = queryWrapperBuilder(thisObj, limitRequest);
        return sysDictDataMapper.selectPage(page, queryWrapper);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    @Cacheable(cacheNames = CacheNames.SYS_DICT, key = "#dictType", unless = "#result == null")
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDataList = sysDictDataMapper.selectList(Wrappers.lambdaQuery(SysDictData.class).eq(SysDictData::getDictType, dictType));
        if (CollUtil.isNotEmpty(dictDataList)) {
            return dictDataList;
        }
        return null;
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.SYS_DICT, key = "#sysDictData.dictType")
    public Boolean saveDictData(SysDictData sysDictData) {
        return sysDictDataMapper.insert(sysDictData) > 0;
    }


    @Override
    @CacheEvict(cacheNames = CacheNames.SYS_DICT, key = "#sysDictData.dictType")
    public Boolean insertOrUpdate(SysDictData sysDictData) {
        return sysDictDataMapper.insertOrUpdate(sysDictData);
    }

    @CacheEvict(cacheNames = CacheNames.SYS_DICT, key = "#sysDictData.dictType")
    @Override
    public Boolean delDicData(SysDictData sysDictData) {
        return sysDictDataMapper.deleteById(sysDictData.getDictCode()) > 0;
    }

    @Override
    public Boolean delDicDataBatch(List<SysDictData> sysDictDataId) {
        List<Long> list = sysDictDataId.stream().map(SysDictData::getDictCode).toList();
        return sysDictDataMapper.deleteByIds(list) > 0;
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.SYS_DICT, key = "#dictType")
    public Boolean clearCacheByType(String dictType) {
        return true;
    }


    /**
     * Description:  批量新增优化版
     *
     * @param entityList entityList
     * @author: makeJava
     * @date: 2026-12-26 11:20:03
     * @return: Integer
     */
    @Override
    public Integer saveBatchByEasyBaseMapper(List<SysDictData> entityList) {

        return sysDictDataMapper.insertBatchSomeColumn(entityList);
    }

    @Cacheable(cacheNames = CacheNames.SYS_DICT, key = "#dictType", unless = "#result == null")
    @Override
    public List<SysDictData> getDictDataByDictType(String dictType) {
        return sysDictDataMapper.selectList(Wrappers.lambdaQuery(SysDictData.class).eq(SysDictData::getDictType, dictType));
    }


    /**
     * 往上传递 SqlSessionFactory
     *
     * @return SqlSessionFactory
     */
    @Override
    public SqlSessionFactory getSqlSessionFactory() {
        return super.getSqlSessionFactory();
    }

    /**
     * 往上传递 Mapper
     *
     * @return Mapper
     */
    @Override
    public Class<?> getThisMapperClass() {
        return SysDictDataMapper.class;
    }

    /**
     * 往上传递 Transaction
     *
     * @return Transaction
     */
    @Override
    public TransactionTemplate getSpringTx() {
        return SpringBeanUtil.getBean(TransactionTemplate.class);
    }
}


